<?php


namespace app\lib;


use app\ErrorCode;
use app\lib\exception\APIException;
use app\lib\exception\ParamException;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use Throwable;
use think\exception\Handle;
use think\exception\HttpException;
use think\exception\ValidateException;
use think\Response;


class Http extends Handle
{
    public function render($request, Throwable $e): Response
    {
        //id不存在
        if ($e instanceof ModelNotFoundException)
        {
            abort(404);
        }

        //APi异常
        if ($e instanceof APIException)
        {
            return Response::create([
                'msg' => $e->getMessage(),
                'code' => $e->getCode(),
                'data' => [],
            ], 'json');
        }

        // 其他错误交给系统处理
        return parent::render($request, $e);
    }
}